import {
  businessOrder,
  searchReplay
} from '../../utils/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    orderData: {
      token: "",
      p: 1,
      state: "",
    },
    arrList: [],
    isdata: true,
    searchData:{
       token:"",
      orderID:""
    }
  },
  //选择查看
  searchBtn(e){
    let orderID ="searchData.orderID"
    this.setData({
      [orderID]: e.currentTarget.dataset.id.orderID
    })
    searchReplay(this.data.searchData).then(res=>{
      var data =JSON.stringify(res.datas)
      wx.navigateTo({
        url: './searchreplay/searchreplay?data=' + data,
      })
    })
  },
  //swiper切换时会调用
  pagechange: function (e) {
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex
      currentPageIndex = (currentPageIndex + 1) % 2
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },
  //用户点击tab时调用
  titleClick: function (e) {
    var that = this
    let state = e.currentTarget.dataset.state
    let orderDataState = "orderData.state";
    let orderDataP = "orderData.p";
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx,
      [orderDataState]: state,
      [orderDataP]: 1,
      arrList: []
    })
    that.getOrder()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight * 2
        });
      }
    });
    wx.getStorage({
      key: 'token',
      success: function (res) {
        let orderDataToken = "orderData.token"; 
        let searchDataToken = "searchData.token";
        that.setData({
          [orderDataToken]: res.data,
          [searchDataToken]: res.data
        })
        that.getOrder()
      },
    })
  },
  getOrder() {
    businessOrder(this.data.orderData).then(res => {
      if (res.code == 200) {
        this.setData({
          arrList: this.data.arrList.concat(res.datas.shop_order) 
        })
        if (res.datas.more == false) {
          this.setData({
            isdata: false
          })
        }
      } else {
        this.setData({
          arrList: []
        })
      }
    })
  },
  //滚动事件bindDownLoad
  bindDownLoad(e) {
    if (this.data.isdata) {
      this.data.orderData.p++
      this.getOrder()
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})